<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\facade\Session;
use think\Request;
use think\facade\View;
use think\facade\{Db};

use app\admin\model\{Menu, Users, FinanceRecord, Role};

class IndexController extends BaseController
{
    public function index()
    {
        $admin = Session::get('token');

        $menuModel = new Menu();
        $roleModel = new Role();

        $where = ['pid' => 0, 'is_show' => 1];
        $menu = $menuModel->getList($where, 0)['data'];

        foreach ($menu as $key => $value) {
            $menu[$key]['list'] = $menuModel->getList(['pid' => $value['id'], 'is_show' => 1], 0)['data'];
        }

        if($admin['role_id'] > 0){
            $role_info = $roleModel->getFind(['id' => $admin['role_id']])['data'];

            $role_arr = explode(',', $role_info->getData('menu'));
            array_pop($role_arr);
        }else{
            $role_arr = [];
        }

        View::assign('admin', $admin);
        View::assign('menu', $menu);
        View::assign('role', $role_arr);
        return view();
    }

    public function home()
    {
        $usersModel =  new Users();
        $finModel   =  new FinanceRecord();

        $admin = Session::get('token');

        if($admin['role_id'] == 0){
            // 总会员量
            $users_count = $usersModel->getNum([])['data'];

            // 新会员量
            $users_new = $usersModel->getNum([['create_time', '>', strtotime(date('Y-m-d', time()))]])['data'];

            $money_where = ['user_id' => 0];
            $order_where = ['status' => 2];
            $order_group_where = ['status' => 3];
            $admin_where = ['role_id' => 2, 'del_time' => 0];
        }else if($admin['role_id'] == 1){
            $admin_ids = Db::name('admin')->where(['pid' => $admin['id']])->column('id');
            $users_count = Db::name('user_shop')->where([['admin_id', 'in', $admin_ids]])->count();
            $users_new = Db::name('user_shop')->where([['admin_id', 'in', $admin_ids], ['create_time', '>', strtotime(date('Y-m-d', time()))]])->count();

            $money_where = [['user_id', '=', 0], ['admin_id', 'in', $admin_ids]];
            $order_where = [['status', '=', 2], ['admin_id', 'in', $admin_ids]];
            $order_group_where = [['status', '=', 3], ['admin_id', 'in', $admin_ids]];
            $admin_where = ['role_id' => 2, 'del_time' => 0, 'pid' => $admin['id']];
        }else{
            $users_count = Db::name('user_shop')->where(['admin_id' => $admin['id']])->count();
            $users_new = Db::name('user_shop')->where([['admin_id', '=', $admin['id']], ['create_time', '>', strtotime(date('Y-m-d', time()))]])->count();

            $money_where = ['user_id' => 0, 'admin_id' => $admin['id']];
            $order_where = ['status' => 2, 'admin_id' => $admin['id']];
            $order_group_where = ['status' => 3, 'admin_id' => $admin['id']];
            $admin_where = ['role_id' => 2, 'del_time' => 0, 'id' => $admin['id']];
        }

        // 总营收
        $money = $finModel->getSum($money_where, 'number')['data'];

        /** 总下单量 */
        $order_num = Db::name('order')->where($order_where)->count();
        $order_group_num = Db::name('order_group')->where($order_group_where)->count();

        /** 门店营收列表 */
        $admin_list = Db::name('admin')->where($admin_where)->limit(10)->order('money DESC')->select();

        $data = [
            'users_count' => $users_count,
            'users_new'   => $users_new,
            'users_old'   => $users_count - $users_new,
            'money'       => $money,
            'order_num'   => $order_num + $order_group_num,
            'admin_list'  => $admin_list,
        ];

        View::assign('data', $data);
        View::assign('admin', $admin);
        return view();
    }

    public function getShopTurnover()
    {

    }
}